package com.lightkits.wms.domain.deliveryorder;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.domain.deliveryorder.service.SaveDeliveryDomain;
import com.lightkits.wms.dto.deliveryorder.SaveDeliveryOrderRequestType;
import com.lightkits.wms.dto.deliveryorder.SaveDeliveryOrderResponseType;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * SaveDeliveryOrderExecutor
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/2/22 15:11
 */
@Component
public class SaveDeliveryOrderExecutor implements ServiceExecutor<SaveDeliveryOrderRequestType,SaveDeliveryOrderResponseType> {
  private final SaveDeliveryDomain saveDeliveryDomain;

    @Autowired
    public SaveDeliveryOrderExecutor(SaveDeliveryDomain saveDeliveryDomain) {
        this.saveDeliveryDomain = saveDeliveryDomain;
    }

    @Override
    public SaveDeliveryOrderResponseType service(SaveDeliveryOrderRequestType request){
      SaveDeliveryOrderResponseType response=new SaveDeliveryOrderResponseType();
      response.setSuccessful(true);
      try{
          response=saveDeliveryDomain.insertValue(request.getDeliveryOrders());
      }catch (LightKitsBizException e) {
          response.setSuccessful(false);
          String mes= StringUtils.substringAfterLast(e.getMessage(),":");
          response.setMessage(ObjectUtil.isNullOrEmpty(mes)?e.getMessage():mes);
      } catch (Exception e) {
          response.setSuccessful(false);
          response.setMessage("保存发货单失败");
      }
        return response;
    }
}
